<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/10/4
 * Time: 13:16
 */

namespace App\Plugin\HostServer\Models;


use Illuminate\Database\Eloquent\SoftDeletes;

class PnHostSvDomainOrder extends HostServerBase
{
    use SoftDeletes;
    public static function getStatus($field=''){
        $type=[
            0=>'未支付',
            1=>'完成',
            2=>'退款',
            3=>'待审核',
            4=>'续费待审核'
        ];
        return $field==''?$type:$type[$field];
    }
    /**
     * 取得会员
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(PnHostSvUser::class, 'user_id', 'id');
    }

    /**
     * 取得type 名字
     * @return array|mixed
     */
    public function getStatusNameAttribute()
    {
        return static::getStatus($this->status);
    }
    /**
     * 取得订单
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order()
    {
        return $this->belongsTo(PnHostSvOrder::class, 'order_id', 'id');
    }

    /**
     * 购买domain
     * @param $order_data
     * @param $system_data
     * @param $user
     */
    public static function buyServer($order_data,$system_data,$user){
        $order=self::create($order_data);
        $system_data['order_id']=$order->id;
        $decs_user_money=$user['money']-$order['total'];
        $user_money_data=[
            'user_id'=>$user['id'],
            'before_money'=>$user['money'],
            'after_money'=>$decs_user_money,
            'type'=>6,//注册域名
            'model_type'=>'pn_host_sv_domain_order',
            'model_id'=>$order->id,
            'money'=>$order['total'],
            'mark'=>'注册'.$order->name
        ];
        $sr=PnHostSvSystemLog::add($system_data);
        $mr=PnHostSvUserMoneyLog::add($user_money_data);
        //扣除会员金额
        $user->money=$decs_user_money;
        $ur=$user->save();
        if($sr && $order && $mr && $ur)
        {
            return $order;
        }else
        {
            return false;
        }

    }

    /**
     * 续费订单
     * @param $money 续费金额
     * @param $order 原来订单
     * @param $month 月数
     * @param $user 会员
     * @return bool
     */
    public static function renewalGood($money,$order,$year,$user){
        $order->end_at=date('Y-m-d H:i:s', strtotime($year . " year",strtotime($order['end_at'])));
        $order->year_numbers=$order->year_numbers+$year;//年累计
        $order->check_year_numbers=$year;//续费的年份
        $order->total=$order->total+$money;//总金额累计
        $r=$order->save();
        $decs_user_money=$user['money']-($year*$money);
        $user_money_data=[
            'user_id'=>$user['id'],
            'before_money'=>$user['money'],
            'after_money'=>$decs_user_money,
            'type'=>7,//域名续费
            'model_type'=>'pn_host_server_order',
            'model_id'=>$order->id,
            'money'=>$money,//续费金额
            'mark'=>'续费'.$order->name
        ];

        $system_data = [
            'user_id' => $user['id'],
            'money' => $money,// 续费金额
            'name' => '续费服务器',
            'pay_ok_at' => date('Y-m-d H:i:s'),
            'status' => '1',
            'type' => '6',//续费服务器
            'order_id'=>$order->id
        ];
        //系统资金表
        $sr=PnHostSvSystemLog::add($system_data);
        //会员变动表
        $mr=PnHostSvUserMoneyLog::add($user_money_data);
        //扣除会员余额
        $user->money=$decs_user_money;
        $ur=$user->save();
        return $r && $sr && $mr && $ur;

    }
    //订单来源
    public function from(){
        return $this->morphTo('from','model_type','model_id');
    }
    //退款域名
    public static function refund($order,$user){
        $decs_user_money=$user['money']+$order['total'];
        $money=$order['total'];
        $user_money_data=[
            'user_id'=>$user['id'],
            'before_money'=>$user['money'],
            'after_money'=>$decs_user_money,
            'type'=>5,//退款
            'model_type'=>'pn_host_server_order',
            'model_id'=>$order->id,
            'money'=>$money,//金额
            'mark'=>$order->name.'退款,资金退回'
        ];
        //会员变动表
        $mr=PnHostSvUserMoneyLog::add($user_money_data);
        $user->money=$decs_user_money;
        $u_r=$user->save();

        $system_data = [
            'user_id' => $user['id'],
            'money' => $money,// 退款金额
            'name' => '退款域名',
            'pay_ok_at' => date('Y-m-d H:i:s'),
            'status' => '1',
            'type' => '4',//退款
            'order_id'=>$order->id
        ];
        //系统资金表
        $sr=PnHostSvSystemLog::add($system_data);

        return $u_r && $mr && $sr;

    }
    //续费金额
    public static function renewalMoney($money){
        $ratio = config_cache_default('host_server.domain_renewal_rate', 10);
        $renewal_price = round(((100 + $ratio) * $money / 100), 0);
        return $renewal_price;

    }
    //取得域名配置信息
    public function info(){
        return $this->hasOne(PnHostSvDomainSeting::class,'order_id','id');
    }
}